﻿using java.lang;
using java.util;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media.Imaging;
using yzrilyzr.crypto.algorithms.asymmetric.ec;
using yzrilyzr.FAQ.client;
using yzrilyzr.FAQ.constants;
using yzrilyzr.FAQ.utils;
using MessageBox = System.Windows.MessageBox;

namespace FAQ_UI
{
	/// <summary>
	/// NewAccount.xaml 的交互逻辑
	/// </summary>
	public partial class NewAccount : Window, INotifyPropertyChanged
	{
		private Dictionary<string, NamedCurve> curves = new Dictionary<string, NamedCurve>();

		public NewAccount()
		{
			InitializeComponent();
			DataContext = this;
			List<string> algs = new List<string>();
			addAlgorithm(algs, CurveDB.curvesP);
			addAlgorithm(algs, CurveDB.curvesPD);
			addAlgorithm(algs, CurveDB.curvesB);
			addAlgorithm(algs, CurveDB.curvesBD);
			algs.Sort();
			algorithms = new ObservableCollection<string>(algs);
			BSelectIndex = algorithms.IndexOf("secp256r1");
		}

		public void addAlgorithm(List<string> algs, HashMap hm)
		{
			Iterator iter = hm.values().iterator();
			while (iter.hasNext())
			{
				NamedCurve entry = (NamedCurve)iter.next();
				curves[entry.getName().stdName()] = entry;
				algs.Add(entry.getName().stdName());
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		protected void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		public FAQID faqid = FAQID.random();

		public string faqidStr
		{ get { return faqid.toString(); } }

		public string faqidInt
		{ get { return faqid.toInt() + ""; } }

		public string BName { get; set; }
		public string BDescription { get; set; }
		public int BSelectIndex { get; set; }
		public ObservableCollection<string> algorithms = new ObservableCollection<string>();
		private byte[] imageArr;

		public ObservableCollection<string> BAlgorithms
		{
			get { return algorithms; }
		}

		private void randomFAQID(object sender, RoutedEventArgs e)
		{
			faqid = FAQID.random();
			OnPropertyChanged(nameof(faqidStr));
			OnPropertyChanged(nameof(faqidInt));
		}

		private void newAccount(object sender, RoutedEventArgs e)
		{
			try
			{
				int uid = faqid.toInt();
				DB db = DB.from(FAQClient.getClientPath() + "/" + uid + "/userdata", FAQTypeMap.INSTANCE);
				db.put(D.UUID, new Integer(uid), faqid);
				NamedCurve curve = curves[algorithms[BSelectIndex]];
				ECKeyPair kp = new ECKeyPair(curve);
				db.put(D.MY_P2PKEY, K.PUBKEY, kp.getPublicKey().getEncoded());
				db.put(D.MY_P2PKEY, K.PRIKEY, kp.getPrivateKey().getEncoded());
				if (BName != "")
				{
					FAQClient.getPubUsrDB().put(D.NAMES, faqid, BName);
					db.put(D.NAMES, faqid, BName);
				}
				if (BDescription != "")
				{
					FAQClient.getPubUsrDB().put(D.DESCRIPTIONS, faqid, BDescription);
					db.put(D.DESCRIPTIONS, faqid, BDescription);
				}
				if (imageArr != null)
				{
					FAQClient.getPubUsrDB().put(D.HEADS, faqid, imageArr);
					db.put(D.HEADS, faqid, imageArr);
				}
				db.save();
				db.close();
				FAQClient.getPubUsrDB().save();
				MessageBox.Show("创建成功", "FAQ新建帐户", MessageBoxButton.OK);
				Close();
			}
			catch (System.Exception ex)
			{
				MessageBox.Show("创建失败", "FAQ新建帐户", MessageBoxButton.OK, MessageBoxImage.Error);
			}
		}

		private void Image_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			ImageCropWindow w = new ImageCropWindow(1);
			w.ShowDialog();
			CroppedBitmap b = w.getResult();
			if (b != null)
			{
				imageArr = FAQUtil.ToByteArray(new PngBitmapEncoder(), b);
				Avatar.Drawable = FAQUtil.getDrawable(imageArr);
			}
		}
	}
}